/*     */ package com.zimbra.cs.fb;
/*     */ 
/*     */ import com.zimbra.common.account.Key.AccountBy;
/*     */ import com.zimbra.common.calendar.ZCalendar.ICalTok;
/*     */ import com.zimbra.common.calendar.ZCalendar.ZCalendarBuilder;
/*     */ import com.zimbra.common.calendar.ZCalendar.ZComponent;
/*     */ import com.zimbra.common.calendar.ZCalendar.ZVCalendar;
/*     */ import com.zimbra.common.httpclient.HttpClientUtil;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.common.soap.Element.ElementFactory;
/*     */ import com.zimbra.common.soap.MailConstants;
/*     */ import com.zimbra.common.soap.SoapFaultException;
/*     */ import com.zimbra.common.soap.SoapProtocol;
/*     */ import com.zimbra.common.util.ByteUtil;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraHttpConnectionManager;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.AuthToken;
/*     */ import com.zimbra.cs.account.AuthTokenException;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.Server;
/*     */ import com.zimbra.cs.httpclient.HttpProxyUtil;
/*     */ import com.zimbra.cs.mailbox.MailItem.Type;
/*     */ import com.zimbra.cs.service.UserServlet;
/*     */ import com.zimbra.cs.service.mail.ToXML;
/*     */ import com.zimbra.soap.DocumentHandler;
/*     */ import com.zimbra.soap.ProxyTarget;
/*     */ import com.zimbra.soap.ZimbraSoapContext;
/*     */ import java.io.IOException;
/*     */ import java.io.UnsupportedEncodingException;
/*     */ import java.net.URLEncoder;
/*     */ import java.util.ArrayList;
/*     */ import java.util.EnumSet;
/*     */ import java.util.HashMap;
/*     */ import java.util.Iterator;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import java.util.Map.Entry;
/*     */ import java.util.Set;
/*     */ import javax.servlet.http.HttpServletRequest;
/*     */ import org.apache.commons.httpclient.HttpClient;
/*     */ import org.apache.commons.httpclient.HttpMethod;
/*     */ import org.apache.commons.httpclient.methods.GetMethod;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class RemoteFreeBusyProvider
/*     */   extends FreeBusyProvider
/*     */ {
/*     */   private static final String REMOTE = "REMOTE";
/*     */   private Map<String, StringBuilder> mRemoteAccountMap;
/*     */   private ArrayList<FreeBusyProvider.Request> mRequestList;
/*     */   private HttpServletRequest mHttpReq;
/*     */   private ZimbraSoapContext mSoapCtxt;
/*     */   private long mStart;
/*     */   private long mEnd;
/*     */   private String mExApptUid;
/*     */   
/*     */   public RemoteFreeBusyProvider(HttpServletRequest httpReq, ZimbraSoapContext zsc, long start, long end, String exApptUid)
/*     */   {
/*  67 */     this.mRemoteAccountMap = new HashMap();
/*  68 */     this.mRequestList = new ArrayList();
/*  69 */     this.mHttpReq = httpReq;
/*  70 */     this.mSoapCtxt = zsc;
/*  71 */     this.mStart = start;
/*  72 */     this.mEnd = end;
/*  73 */     this.mExApptUid = exApptUid;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public FreeBusyProvider getInstance()
/*     */   {
/*  83 */     return null;
/*     */   }
/*     */   
/*     */   public void addFreeBusyRequest(FreeBusyProvider.Request req)
/*     */   {
/*  88 */     Account account = (Account)req.data;
/*  89 */     if (account == null)
/*  90 */       return;
/*  91 */     String hostname = account.getAttr("zimbraMailHost");
/*  92 */     StringBuilder buf = (StringBuilder)this.mRemoteAccountMap.get(hostname);
/*  93 */     if (buf == null) {
/*  94 */       buf = new StringBuilder(req.email);
/*     */     } else
/*  96 */       buf.append(",").append(req.email);
/*  97 */     this.mRemoteAccountMap.put(hostname, buf);
/*  98 */     this.mRequestList.add(req);
/*     */   }
/*     */   
/* 101 */   public void addFreeBusyRequest(Account requestor, Account acct, String id, long start, long end, int folder) { FreeBusyProvider.Request req = new FreeBusyProvider.Request(requestor, id, start, end, folder);
/* 102 */     req.data = acct;
/* 103 */     addFreeBusyRequest(req);
/*     */   }
/*     */   
/*     */   public List<FreeBusy> getResults()
/*     */   {
/* 108 */     ArrayList<FreeBusy> fbList = new ArrayList();
/* 109 */     for (FreeBusyProvider.Request req : this.mRequestList) {
/* 110 */       HttpMethod method = null;
/* 111 */       Account acct = (Account)req.data;
/*     */       try {
/* 113 */         StringBuilder targetUrl = new StringBuilder();
/* 114 */         targetUrl.append(UserServlet.getRestUrl(acct));
/* 115 */         targetUrl.append("/Calendar?fmt=ifb");
/* 116 */         targetUrl.append("&start=").append(this.mStart);
/* 117 */         targetUrl.append("&end=").append(this.mEnd);
/* 118 */         if (req.folder != -1)
/* 119 */           targetUrl.append("&").append("fbcal").append("=").append(req.folder);
/*     */         try {
/* 121 */           if (this.mExApptUid != null)
/* 122 */             targetUrl.append("&").append("exuid").append("=").append(URLEncoder.encode(this.mExApptUid, "UTF-8"));
/*     */         } catch (UnsupportedEncodingException e) {}
/* 124 */         String authToken = null;
/*     */         try {
/* 126 */           if (this.mSoapCtxt != null)
/* 127 */             authToken = this.mSoapCtxt.getAuthToken().getEncoded();
/*     */         } catch (AuthTokenException e) {}
/* 129 */         if (authToken != null) {
/* 130 */           targetUrl.append("&").append("zauthtoken").append("=");
/*     */           try {
/* 132 */             targetUrl.append(URLEncoder.encode(authToken, "UTF-8"));
/*     */           } catch (UnsupportedEncodingException e) {}
/*     */         }
/* 135 */         HttpClient client = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient();
/* 136 */         HttpProxyUtil.configureProxy(client);
/* 137 */         method = new GetMethod(targetUrl.toString());
/*     */         String fbMsg;
/*     */         try {
/* 140 */           HttpClientUtil.executeMethod(client, method);
/* 141 */           byte[] buf = ByteUtil.getContent(method.getResponseBodyAsStream(), 0);
/* 142 */           fbMsg = new String(buf, "UTF-8");
/*     */         }
/*     */         catch (IOException ex) {
/* 145 */           fbMsg = null;
/*     */         }
/* 147 */         if (fbMsg != null) {
/* 148 */           ZCalendar.ZVCalendar cal = ZCalendar.ZCalendarBuilder.build(fbMsg);
/* 149 */           for (compIter = cal.getComponentIterator(); compIter.hasNext();) {
/* 150 */             ZCalendar.ZComponent comp = (ZCalendar.ZComponent)compIter.next();
/* 151 */             if (ZCalendar.ICalTok.VFREEBUSY.equals(comp.getTok())) {
/* 152 */               FreeBusy fb = FreeBusy.parse(comp);
/* 153 */               fbList.add(fb);
/*     */             }
/*     */           }
/*     */         }
/*     */       } catch (ServiceException e) { Iterator<ZCalendar.ZComponent> compIter;
/* 158 */         ZimbraLog.fb.warn("can't get free/busy information for " + req.email, e);
/*     */       } finally {
/* 160 */         if (method != null)
/* 161 */           method.releaseConnection();
/*     */       }
/*     */     }
/* 164 */     return fbList;
/*     */   }
/*     */   
/*     */   public Set<MailItem.Type> registerForItemTypes()
/*     */   {
/* 169 */     return EnumSet.noneOf(MailItem.Type.class);
/*     */   }
/*     */   
/*     */   public boolean registerForMailboxChanges()
/*     */   {
/* 174 */     return false;
/*     */   }
/*     */   
/*     */   public boolean registerForMailboxChanges(String accountId)
/*     */   {
/* 179 */     return false;
/*     */   }
/*     */   
/*     */   public long cachedFreeBusyStartTime()
/*     */   {
/* 184 */     return 0L;
/*     */   }
/*     */   
/*     */   public long cachedFreeBusyEndTime()
/*     */   {
/* 189 */     return 0L;
/*     */   }
/*     */   
/*     */   public long cachedFreeBusyStartTime(String accountId)
/*     */   {
/* 194 */     return 0L;
/*     */   }
/*     */   
/*     */   public long cachedFreeBusyEndTime(String accountId)
/*     */   {
/* 199 */     return 0L;
/*     */   }
/*     */   
/*     */   public boolean handleMailboxChange(String accountId)
/*     */   {
/* 204 */     return true;
/*     */   }
/*     */   
/*     */   public String foreignPrincipalPrefix()
/*     */   {
/* 209 */     return "";
/*     */   }
/*     */   
/*     */   public void addResults(Element response)
/*     */   {
/* 214 */     Provisioning prov = Provisioning.getInstance();
/* 215 */     for (Map.Entry<String, StringBuilder> entry : this.mRemoteAccountMap.entrySet())
/*     */     {
/* 217 */       String paramStr = ((StringBuilder)entry.getValue()).toString();
/* 218 */       String[] idStrs = paramStr.split(",");
/*     */       try
/*     */       {
/* 221 */         Element req = this.mSoapCtxt.getRequestProtocol().getFactory().createElement(MailConstants.GET_FREE_BUSY_REQUEST);
/* 222 */         req.addAttribute("s", this.mStart);
/* 223 */         req.addAttribute("e", this.mEnd);
/* 224 */         req.addAttribute("uid", paramStr);
/*     */         
/*     */ 
/* 227 */         Account acct = prov.get(Key.AccountBy.name, idStrs[0], this.mSoapCtxt.getAuthToken());
/* 228 */         if (acct == null)
/* 229 */           acct = prov.get(Key.AccountBy.id, idStrs[0], this.mSoapCtxt.getAuthToken());
/* 230 */         if (acct != null) {
/* 231 */           Element remoteResponse = proxyRequest(req, acct.getId(), this.mSoapCtxt);
/* 232 */           for (Element thisElt : remoteResponse.listElements())
/* 233 */             response.addElement(thisElt.detach());
/*     */         } else {
/* 235 */           ZimbraLog.fb.debug("Account " + idStrs[0] + " not found while searching free/busy");
/*     */         }
/*     */       } catch (SoapFaultException e) {
/* 238 */         ZimbraLog.fb.error("cannot get free/busy for " + idStrs[0], e);
/* 239 */         addFailedAccounts(response, idStrs);
/*     */       } catch (ServiceException e) {
/* 241 */         ZimbraLog.fb.error("cannot get free/busy for " + idStrs[0], e);
/* 242 */         addFailedAccounts(response, idStrs);
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public String getName()
/*     */   {
/* 251 */     return "REMOTE";
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private void addFailedAccounts(Element response, String[] idStrs)
/*     */   {
/* 262 */     for (String id : idStrs) {
/* 263 */       ToXML.encodeFreeBusy(response, FreeBusy.nodataFreeBusy(id, this.mStart, this.mEnd));
/*     */     }
/*     */   }
/*     */   
/*     */   protected Element proxyRequest(Element request, String acctId, ZimbraSoapContext zsc) throws ServiceException
/*     */   {
/* 269 */     ZimbraSoapContext zscTarget = new ZimbraSoapContext(zsc, acctId);
/* 270 */     Provisioning prov = Provisioning.getInstance();
/* 271 */     Account acct = prov.get(Key.AccountBy.id, acctId);
/* 272 */     Server server = prov.getServer(acct);
/*     */     
/*     */ 
/* 275 */     ProxyTarget proxy = new ProxyTarget(server.getId(), zsc.getAuthToken(), this.mHttpReq);
/* 276 */     Element response = DocumentHandler.proxyWithNotification(request.detach(), proxy, zscTarget, zsc);
/* 277 */     return response;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/fb/RemoteFreeBusyProvider.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */